package grsu.epam.airline.action;

import grsu.epam.airline.form.PlaneModifyForm;
import grsu.epam.airline.service.PlaneService;
import grsu.epam.airline.service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class PlaneUpdateAction extends Action {
	String target = ForwardActionTo.PLANES;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			PlaneModifyForm input = (PlaneModifyForm) form;
			PlaneService planeService = new PlaneService();
			planeService.updatePlane(input.getPlaneId(), input.getModel());
			request.setAttribute("planes", planeService.getAllPlane());
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}
}
